package controller
{
	import away3d.containers.ObjectContainer3D;
	import away3d.events.MouseEvent3D;
	
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.ui.Keyboard;
	
	import gameObject.GameObjectBase;
	import gameObject.MapBase;
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.MapResource;
	
	import tool.tweener.Tweener;

	public class MapCameraController extends GameControllerBase
	{
		public var speed:Number = 150;
		
		private var _ground:GameMesh;
		private var _cameraRoot:ObjectContainer3D;
		
		private var _mark:Sprite = new Sprite;
		
		private var _alphaCross:GameMesh;
		private var _offset:Vector3D = new Vector3D;
		
		public function MapCameraController(){}
		
		override public function setup(target:GameObjectBase):void
		{
			super.setup(target);
			
			var map:MapBase = target as MapBase;
			_cameraRoot = map.cameraRoot;
			
			var res:MapResource = target.resource as MapResource;
			_ground = res.groundMesh;
		}
		
		override public function dispose():void
		{
			_ground.mouseEnabled = false;
			clearListener();
			_target.controller = null;
		}
		
		public function testMouseCamera():void
		{
			clearListener();
			
			_ground.mouseEnabled = true;
			_ground.addEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			
			Global.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		public function mouseRotateCamera():void
		{
			Global.stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.W:
					_offset.y++;
					break;
				case Keyboard.S:
					_offset.y--;
					break;	
				default:
					break;
			}
		}
		
		public function followObject(Object:GameObjectBase):void
		{
			clearListener();
			Object.addChild(_cameraRoot);
		}
		
		public function goto(targetPos:Vector3D,time:Number):void
		{
			clearListener();
			
			Tweener.addTween(_cameraRoot,{position:targetPos,time:time,transition:"easeoutsine"});
		}
		
		///////////////////////////////////////////////////
		private function clearListener():void
		{
			if(_cameraRoot.parent)_cameraRoot.parent.removeChild(_cameraRoot);
			
			Global.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			_ground.removeEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		private function onMouseDown(e:MouseEvent):void
		{
			_mark.x = Global.stage.mouseX;
			_mark.y = Global.stage.mouseY;
			_mark.rotationY = _cameraRoot.rotationY;
			_mark.rotationX = _cameraRoot.rotationX;
			
			Global.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			Global.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		private function onMouseMove(e:MouseEvent):void
		{
			_cameraRoot.rotationY = _mark.rotationY+0.25*(Global.stage.mouseX-_mark.x);
			_cameraRoot.rotationX = _mark.rotationX+0.25*(Global.stage.mouseY-_mark.y);
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function onDoubleClick(e:MouseEvent3D):void
		{
			var pos:Vector3D = e.localPosition;
			
			var sub:Vector3D = pos.subtract(_cameraRoot.position);
			var dis:Number = sub.lengthSquared;
			dis = Math.sqrt(dis);
			
			var t:Number = dis/speed;
			Tweener.removeTweens(_cameraRoot);
			Tweener.addTween(_cameraRoot,{x:pos.x,z:pos.z,time:t,transition:"easeoutsine"});
		}
		
	}
}